class Solution {
public:
    void reverse(string& s, int begin, int end)
    {
        for (int i = begin, j = end; i < j; i++, j--)
            swap(s[i], s[j]);
    }

    string reverseStr(string s, int k) {
        int len = s.size();
        for (int i = 0; i < len; i += 2 * k)
        {
            if (i + k <= len)
            {
                reverse(s, i, i + k - 1);
            }
            else
                reverse(s, i, len - 1);
        }
        return s;
    }
};